package com.sample.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DiscretePathEffect;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.base.sampleproject.R;

/**
 * Created by devnn on 16/1/20.
 */
public class TestView extends View {
    private String TAG = this.getClass().getSimpleName();
    private Paint paint;
    private Context context;
    private Rect textBound = new Rect();

    public TestView(Context context) {
        super(context);
        this.context = context;
    }

    public TestView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        paint = new Paint();

    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        Log.i(TAG, "onLayout:" + changed);
        Log.i(TAG, "onLayout left:" + left);
        Log.i(TAG, "onLayout top:" + top);
        Log.i(TAG, "onLayout right:" + right);
        Log.i(TAG, "onLayout bottom:" + bottom);
        super.onLayout(changed, left, top, right, bottom);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        Log.i(TAG, "onMeasure()");
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //画边框
        paint.setColor(ContextCompat.getColor(context, R.color.color_deep_green));
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(5);
        canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), paint);


        paint.setAlpha(255);
        //画内切圆
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(ContextCompat.getColor(context, R.color.color_green));
        paint.setAntiAlias(true);
        canvas.drawCircle(getWidth() / 2, getHeight() / 2, getWidth() / 2 - 5, paint);

        //画内切圆的内切正方形
        paint.setStyle(Paint.Style.FILL);
        Shader shader=new LinearGradient(0,10,100,10,new int[]{ContextCompat.getColor(context, R.color.color_red),ContextCompat.getColor(context, R.color.color_deep_green)},null,Shader.TileMode.REPEAT);
        paint.setShader(shader);

        int width = (int) (((float) getWidth()) / 1.41);
        canvas.drawRect(getWidth() / 2 - width / 2 + 5, getHeight() / 2 - width / 2 + 5, getWidth() / 2 - width / 2 + width - 5, getHeight() / 2 - width / 2 + width - 5, paint);
        paint.setShader(null);
        //在正中央画字符串
        paint.setColor(ContextCompat.getColor(context, R.color.color_black));
        paint.setTextSize(50);
        String text = "Hello,canvas!";
        paint.getTextBounds(text, 0, text.length(), textBound);
        canvas.drawText(text, getWidth() / 2 - textBound.width() / 2, getHeight() / 2 + textBound.height() / 2, paint);

        //再画一个内切长方形和它的内切椭圆
        RectF rectF = new RectF(getWidth() / 2 - getWidth() * 1.73f / 2 / 2 + 5, getHeight() / 2 - getHeight() / 2 / 2 + 5, getWidth() / 2 - getWidth() * 1.73f / 2 / 2 + getWidth() * 1.73f / 2 - 5, getHeight() / 2 + getHeight() / 2 / 2 - 5);
//        paint.setShader("");
        paint.setStyle(Paint.Style.STROKE);
        canvas.drawRect(rectF, paint);
        paint.setColor(ContextCompat.getColor(context, R.color.color_yellow));
        canvas.drawOval(rectF, paint);
        paint.setStyle(Paint.Style.FILL);
        if (Build.VERSION.SDK_INT >= 21) {
            canvas.drawArc(5, 5, getWidth() - 5, getHeight() - 5, 225, 90, false, paint);
        }
    }
}
